import threading, Queue, random, thread,time
from math import *
# Functions for...
# Game: Add/Remove agent
# Agent Controller: Change active agent, move agent
# This adds functions to the "control_queue" which is a threadsafe queue that will execute the functions passed to it in the main game control loop
# This allows for threadsafe calling of any function in the game's theadsafe region
class human_control(object):
	def __init__(self,graphics_engine,control_queue,agent_controller,world_model,game_model=None,t_graphics=None):
		self.control_queue = control_queue
		self.tk_root=graphics_engine.tk_root
		self.graphics_engine= graphics_engine
		self.agent_controller=agent_controller
		self.world_model=world_model
		self.game_model=game_model
		self.control_queue=control_queue
		self.t_graphics = t_graphics
		self.game_active = False
		self.colors = ['yellowagent', 'blueagent']
		self.chosen_colors = 0
		self.create_menu()
		
	def create_menu(self):
		self.graphics_engine.set_background('menu.jpg')
		self.menu_text = {0: 'Start', 1: 'Options', 2: 'Quit'}
		self.menu_text_pos = [(600, 500), (600, 550), (600, 600)]
		for i in xrange(3):
			self.t_graphics.text_draw(i, self.menu_text_pos[i], self.menu_text[i])
		self.tk_root.bind('<Button-1>',self.choose_event)
	
	def choose_event(self, event):
		x_pos = event.x
		y_pos = event.y
		if x_pos > 650 or x_pos < 550 or y_pos > 625 or y_pos < 475:
			return
		min_dist = 1e10000
		min=0
		for i in xrange(len(self.menu_text_pos)):
			x_diff = x_pos - self.menu_text_pos[i][0]
			y_diff = y_pos - self.menu_text_pos[i][1]
			dist= sqrt(x_diff*x_diff + y_diff*y_diff)
			if dist < min_dist:
				min = i
				min_dist = dist
		if min == 0:
			self.control_queue.put(self.make_game)
		elif min == 1:
			self.control_queue.put(self.color_menu)
		else:
			self.graphics_engine.tk_root.destroy()
			
	def color_menu(self):
	
		for i in xrange(3):
			self.t_graphics.text_die(i)
			
		self.display_text = {0: 'Choose a color for team 1.', 1: 'Choose a color for team 2.'}
		self.display_text_pos = (360,150)

		self.graphics_engine.set_background('colormenu.png')
		self.tk_root.unbind('<Button-1>')
		self.tk_root.bind('<Button-1>', self.choose_colors)
		self.chosen_colors = 0
		
		self.t_graphics.text_draw(0, self.display_text_pos, self.display_text[0])
		
	def choose_colors(self, event):
		x_pos = event.x
		y_pos = event.y
		color_pos = [(300,300),(420,300),(300,420),(420,420)]
		if x_pos > 480 or x_pos < 240 or y_pos > 480 or y_pos < 240:
			return
		min_dist = 1e10000
		min=0
		for i in xrange(4):
			x_diff = x_pos - color_pos[i][0]
			y_diff = y_pos - color_pos[i][1]
			dist= sqrt(x_diff*x_diff + y_diff*y_diff)
			if dist < min_dist:
				min = i
				min_dist = dist
		if min == 0:
			self.colors[self.chosen_colors] = 'blueagent'
		elif min == 1:
			self.colors[self.chosen_colors] = 'yellowagent'
		elif min == 2:
			self.colors[self.chosen_colors] = 'greenagent'
		else:
			self.colors[self.chosen_colors] = 'redagent'
		self.chosen_colors += 1
		
		if self.chosen_colors >= 2:
			self.t_graphics.text_die(0)
			self.tk_root.unbind('<Button-1>')
			self.create_menu()
		else:
			self.t_graphics.text_draw(0, self.display_text_pos, self.display_text[self.chosen_colors])
			
			
	def make_game(self):
		self.tk_root.bind('<Tab>', lambda x:self.control_queue.put(self.agent_controller.next_agent))
		self.tk_root.bind('<Up>',lambda x:self.control_queue.put(self.agent_controller.move_forward))
		self.tk_root.bind('<Down>',lambda x:self.control_queue.put(self.agent_controller.move_backward))
		self.tk_root.bind('<Right>',lambda x:self.control_queue.put(self.agent_controller.rotate_cw))
		self.tk_root.bind('<Left>',lambda x:self.control_queue.put(self.agent_controller.rotate_ccw))
		self.tk_root.bind('<.>',lambda x:self.control_queue.put(self.agent_controller.strafe_right))
		self.tk_root.bind('<,>',lambda x:self.control_queue.put(self.agent_controller.strafe_left))
		self.tk_root.bind('<Shift-Up>',lambda x:self.control_queue.put(self.agent_controller.move_up))
		self.tk_root.bind('<Shift-Down>',lambda x:self.control_queue.put(self.agent_controller.move_down))
		self.tk_root.bind('<Shift-Right>',lambda x:self.control_queue.put(self.agent_controller.move_right))
		self.tk_root.bind('<Shift-Left>',lambda x:self.control_queue.put(self.agent_controller.move_left))
		self.tk_root.bind('<space>', lambda x:self.control_queue.put(self.agent_controller.shoot))
		self.tk_root.bind('<g>', lambda x:self.control_queue.put(self.game_model.switch_graphics))
		self.tk_root.bind('<s>', lambda x:self.control_queue.put(self.game_model.save_ga))
		self.tk_root.bind('<l>', lambda x:self.control_queue.put(self.game_model.load_ga))
		self.graphics_engine.tk_root.unbind('<Button-1>')
		for i in xrange(3):
			self.t_graphics.text_die(i)
			
		# Create Agents, put them in a dict indexed by number, pass them to agent_controller, initialize them with world_model
		self.graphics_engine.set_background('field.png')
		self.game_model.set_team_colors(self.colors)
		self.game_model.create_game_world()
		self.agent_controller.use_agents(self.world_model.objects)
		self.game_active = True
